package com.jie.deletefile.DIYjindutiao;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.jie.deletefile.R;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Justin on 2016/5/20.
 */
public class ClearArcView extends View {

    private Paint paint;
    private RectF oval;

    private int arcColor = 0xFFFF8C00;
    private final int START_ANGLE = -90;
    private int sweepAngle = 0;
    private int state = 0;//0： 回退；1：前进

    //当后退的时候的速度
    private int[] back = new int[]{-6,-6,-10,-10,-10,-12};
    private int backIndex = 0;

    //当前进的时候的速度
    private int[] goon = new int[]{12, 12, 12, 12, 10, 10, 10, 8, 8, 8, 6};
    private int goonIndex = 0;

    //是否在清理的状态，也就是，动画是否在执行
    private boolean isRuning;


    public ClearArcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAngle(360);
        paint = new Paint();
        arcColor = context.getResources().getColor(R.color.clear_arc_color);

    }

    private void setAngle(int angle) {
        sweepAngle = angle;
        postInvalidate();
        isRuning = false;
    }

    /**
     * 设置圆形角度
     * @param angle
     */
    public void setAngleWithAnim(final int angle){
        if(isRuning){
            return;
        }
        isRuning = true;
        state = 0;
        final Timer timer = new Timer();

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                switch (state){
                    case 0:
                        sweepAngle+=back[backIndex++];
                        if(backIndex>=back.length){
                            backIndex = back.length-1;
                        }
                        postInvalidate();
                        if(sweepAngle<=0){
                            sweepAngle=0;
                            state = 1;
                            backIndex = 0;
                        }
                        break;
                    case 1:
                        sweepAngle+=goon[goonIndex++];
                        if(goonIndex>=goon.length){
                            goonIndex = goon.length-1;
                        }
                        postInvalidate();
                        if(sweepAngle>=angle){
                            isRuning = false;
                            goonIndex = 0;
                            sweepAngle = angle;
                            timer.cancel();
                        }

                        break;
                }
            }
        };
        timer.schedule(task,24,24);
    }


    /**
     * 设置圆形颜色
     * @param arcColor
     */
    public void setArcColor(int arcColor) {
        this.arcColor = arcColor;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);

        oval = new RectF(0,0,viewWidth,viewHeight);

        setMeasuredDimension(viewWidth,viewHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.drawArc();
        paint.setColor(arcColor);
        paint.setAntiAlias(true);
        canvas.drawArc(oval,START_ANGLE,sweepAngle,true,paint);


    }
}
